package sms.zj.com.recyclertest.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;


import java.util.Collections;

import butterknife.BindView;
import butterknife.ButterKnife;
import sms.zj.com.recyclertest.R;
import sms.zj.com.recyclertest.func.drag.DefaultItemTouchHelpCallback;
import sms.zj.com.recyclertest.func.drag.DefaultItemTouchHelper;


/**
 * 拖拽排序
 */
public class DragActivity extends BaseActivity {

    private static final String TAG = "DragActivity";

    @BindView(R.id.recyclerView)
    RecyclerView mRecyclerView;
    protected RecyclerViewAdapter mAdapter;

    private DefaultItemTouchHelper itemTouchHelper;


    public static void show(Activity activity) {
        Intent intent = new Intent(activity, DragActivity.class);
        activity.startActivity(intent);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_headerfooter);
        ButterKnife.bind(this);
        initData();

        mAdapter = new RecyclerViewAdapter();
        mRecyclerView.setAdapter(mAdapter);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false));
        // TODO: 2017/1/7 网格布局不能swipe
//        mRecyclerView.setLayoutManager(new GridLayoutManager(this, 2, GridLayoutManager.VERTICAL,false));
        mRecyclerView.setHasFixedSize(true);


        // 把ItemTouchHelper和itemTouchHelper绑定
        itemTouchHelper = new DefaultItemTouchHelper(onItemTouchCallbackListener);
        itemTouchHelper.attachToRecyclerView(mRecyclerView);

        itemTouchHelper.setDragEnable(true);
        itemTouchHelper.setSwipeEnable(true);
    }
    private DefaultItemTouchHelpCallback.OnItemTouchCallbackListener onItemTouchCallbackListener = new DefaultItemTouchHelpCallback.OnItemTouchCallbackListener() {
        @Override
        public void onSwiped(int adapterPosition) {
            if (data != null) {
                data.remove(adapterPosition);
                mAdapter.notifyItemRemoved(adapterPosition);
            }
        }

        @Override
        public boolean onMove(int srcPosition, int targetPosition) {
            if (data != null) {
                // 更换数据源中的数据Item的位置
                Collections.swap(data, srcPosition, targetPosition);

                // 更新UI中的Item的位置，主要是给用户看到交互效果
                mAdapter.notifyItemMoved(srcPosition, targetPosition);
                return true;
            }
            return false;
        }
    };


    public class RecyclerViewAdapter extends RecyclerView.Adapter{
        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(DragActivity.this).inflate(R.layout.item, parent, false);
            DragActivity.RecyclerViewHolder recyclerViewHolder = new DragActivity.RecyclerViewHolder(view);
            return recyclerViewHolder;
        }

        @Override
        public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
            RecyclerViewHolder h = (RecyclerViewHolder) holder;
            String s = data.get(position);
            h.bindData(s);
        }

        @Override
        public int getItemCount() {
            return data.size();
        }

    }
    public class RecyclerViewHolder extends RecyclerView.ViewHolder implements View.OnLongClickListener {
        @BindView(R.id.tv)
        TextView tv;

        public RecyclerViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this,itemView);
            tv.setOnLongClickListener(this);
        }
        public void bindData(String str){
            tv.setText(str);
        }

        @Override
        public boolean onLongClick(View v) {
            itemTouchHelper.startDrag(this);
            return true;
        }
    }

}
